package com.jdy.haoduoaiteacher.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jdy.haoduoaiteacher.MyTeacherApp;
import com.jdy.haoduoaiteacher.adapter.BabyHabitAdapter;
import com.jdy.haoduoaiteacher.adapter.ImageGridAdapter2;
import com.jdy.haoduoaiteacher.adapter.MyPagerAdapter;
import com.jdy.haoduoaiteacher.intface.IAsyncTask;
import com.jdy.haoduoaiteacher.model.BabyHabit;
import com.jdy.haoduoaiteacher.model.LeBabyHabitWrapper;
import com.jdy.haoduoaiteacher.model.LePhotoItemWrapper;
import com.jdy.haoduoaiteacher.model.LeUser;
import com.jdy.haoduoaiteacher.model.PhotoItem;
import com.jdy.haoduoaiteacher.model.StudentDeviceModel;
import com.jdy.haoduoaiteacher.parse.JsonSerializer;
import com.jdy.haoduoaiteacher.pulltorefresh.PullToRefreshLayout;
import com.jdy.haoduoaiteacher.pulltorefresh.PullableGridView;
import com.jdy.haoduoaiteacher.util.CommonUtil;
import com.jdy.haoduoaiteacher.util.CustomAsyncTask;
import com.jdy.haoduoaiteacher.util.HttpUtils;
import com.jdy.haoduoaiteacher.util.IntentConstants;
import com.jdy.haoduoaiteacher.util.ResponseResult;
import com.jdy.haoduoaiteacher.util.Tools;
import com.jdy.haoduoaiteacher.view.RippleView;
import com.jdy.haoduoaiteacher.view.RoundImageView;
import com.jdy.haoduoaiteacher.widget.CustomCommonDialog;
import com.jdy.haoduoaiteacher.widget.CustomEditDialogOne;
import com.jdy.haoduoaiteacher.widget.MyViewPager;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;



/**
 * 宝贝信息
 *
 * @author hf
 */
@SuppressLint({"InflateParams", "HandlerLeak"})
public class MyLoveListActivity extends BaseActivity implements
        OnClickListener, OnPageChangeListener {
    @InjectView(id = R.id.select_cartoon, click = true)
    private RelativeLayout select_cartoon;
    @InjectView(id = R.id.tv_cartoon)
    private TextView tv_cartoon;
    @InjectView(id = R.id.select_radio, click = true)
    private RelativeLayout select_radio;
    @InjectView(id = R.id.tv_radio)
    private TextView tv_radio;
    @InjectView(id = R.id.beacon)
    private ImageView beacon;
    @InjectView(id = R.id.vpager)
    private MyViewPager vpager;
    @InjectView(id = R.id.bottomlyt)
    private LinearLayout bottomlyts;
    @InjectView(id = R.id.delete, click = true)
    private Button delete;
    @InjectView(id = R.id.select, click = true)
    private Button select;
    //    @InjectView(id = R.id.toplyt)
//    private LinearLayout toplyt;
//    @InjectView(id = R.id.bealyt)
//    private LinearLayout bealyt;
    @InjectView(id = R.id.tv_edittt, click = true)
    private TextView tv_edittt;

    private boolean isphoto;

    private View page1;
    private View page2;
    private LayoutInflater mInflater;
    private int offset = 0;
    private int currIndex = 0;
    private List<View> views;
    private DisplayMetrics dm = null;
    private int screenW;
    private LayoutParams params;

    // page1
    private LinearLayout emptylayout1;
    private RippleView reloadRippleView1;
    private PullToRefreshLayout refreshLayout1;
    private ListView listView1;
    private ImageView img_defaultfail1;
    private Button btnReload1;

    // page2
    private LinearLayout emptylayout2;
    private RippleView reloadRippleView2;
    private PullToRefreshLayout refreshLayout2;
    private PullableGridView listView2;
    private ImageView img_defaultfail2;
    private Button btnReload2;

    private BabyHabitAdapter mStoryAdapter;
    private ImageGridAdapter2 mRadioAdapter;

    private LeBabyHabitWrapper mLeFavoriteItemWrapper1;
    private LePhotoItemWrapper /*mLeFavoriteItemWrapper1,*/
            mLeFavoriteItemWrapper2;
    //
    private ArrayList<BabyHabit> mStoryAdapterList;
    private ArrayList<BabyHabit> mStoryArrayList;
    //
    private ArrayList<PhotoItem> mRadioAdapterList;
    private ArrayList<PhotoItem> mRadioArrayList;

    private LeUser mLeUser;
    private int mLastpage = 1;
    private int mLastpage2 = 1;
    public boolean isrefreshlist1 = false;
    public boolean isrefreshlist2 = false;

    @InjectView(id = R.id.home_face_img, click = true)
    private RoundImageView home_face_img;
    @InjectView(id = R.id.parentAccountIdTextView, click = true)
    private TextView parentAccountIdTextView;
    @InjectView(id = R.id.tv_desc, click = true)
    private TextView tv_desc;
    @InjectView(id = R.id.home_top_background)
    private View home_top_background;

    private StudentDeviceModel mStudentDeviceModel;

    @Override
    @SuppressLint("InflateParams")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.mylovelist_teacher);
        // setTitle("我的收藏");

        hideTitleView();

        // initUpRightBtn();
        findViewById(R.id.delete).setOnTouchListener(CommonUtil.VIEW_TOUCH_DARK);


        Intent it = getIntent();
        if (it != null) {
            isphoto = it.getBooleanExtra("isphoto", false);
            mStudentDeviceModel = (StudentDeviceModel) it.getSerializableExtra("studentmodel");
            if (mStudentDeviceModel != null) {
                if (Tools.isNotEmpty(mStudentDeviceModel.machine.avatar)) {
                    Picasso.with(this).load(mStudentDeviceModel.machine.avatar).into(home_face_img);

                }
                parentAccountIdTextView.setText(mStudentDeviceModel.nick_name);
                tv_desc.setText("设备号: " + mStudentDeviceModel.machine_id);
            }
        }

        initialize();
        initHandler();
        initListener();
        InitViewPager();

        loadPhotoData();
        loadStoryData();
        //  loadStoryData();
    }

    //    @SuppressWarnings("unused")
//    private void deleteFavorite(final LeFavoriteItem item) {
//        new CustomAsyncTask(this, new IAsyncTask() {
//            @Override
//            public void onRecieveData(Activity activity, ResponseResult result) {
//                if (result.isSuccess()) {
//                    toastShow("删除成功");
//                    bottomlyts.setVisibility(View.GONE);
//                    vpager.setScanScroll(true);
//                    if (currIndex == 0 && mStoryAdapter != null) {
//                        mStoryAdapter.setEditor(false);
//                        mStoryAdapter.notifyDataSetChanged();
//                    } else if (currIndex == 1 && mRadioAdapter != null) {
//                        mRadioAdapter.setEditor(false);
//                        mRadioAdapter.notifyDataSetChanged();
//                    }
//
//                    toplyt.setVisibility(View.VISIBLE);
//                    bealyt.setVisibility(View.VISIBLE);
//                    getbtn_right().setVisibility(View.GONE);
//                    mBackImageButton.setVisibility(View.VISIBLE);
//                    mLastpage = 1;
//                    isrefreshlist1 = true;
//                    isrefreshlist2 = true;
//                    loadStoryData();
//                } else {
//                    toastShow(result.data);
//                }
//            }
//
//            @Override
//            public ResponseResult doInbackground(Activity activity) {
//                HashMap<String, Object> param_ = new HashMap<String, Object>();
//                param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
//                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
//                return HttpUtils.startRequest(HttpUtils.HOST_API
//                                + "/api/2.0/member/favorite/" + item.id, param_,
//                        HttpUtils.TAG_DELETE_REQUEST);
//            }
//        }).execute();
//    }
//
    private void fillLoveStoryList(boolean loadFail) {
        if (mStoryAdapter == null) {
            if (mStoryArrayList != null && !mStoryArrayList.isEmpty()) {
                mStoryAdapter = new BabyHabitAdapter(MyLoveListActivity.this, mStoryArrayList);
            } else {
                mStoryAdapter = new BabyHabitAdapter(this,
                        new ArrayList<BabyHabit>());
            }

            listView1.setAdapter(mStoryAdapter);
        } else {
            mStoryAdapter.notifyDataSetChanged();

//            if (mStoryArrayList != null
//                    && mStoryArrayList.size() == mLeFavoriteItemWrapper1.total) {
//
//            } else {
//                refreshLayout1.refreshFinish(PullToRefreshLayout.SUCCEED);
//                refreshLayout1.loadmoreFinish(PullToRefreshLayout.SUCCEED);
//            }

            refreshLayout1.refreshFinish(PullToRefreshLayout.LOAD_COMPLETED);
            refreshLayout1.loadmoreFinish(PullToRefreshLayout.LOAD_COMPLETED);

            // refreshLayout1.refreshFinish(PullToRefreshLayout.SUCCEED);
            // refreshLayout1.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }

        if (loadFail) {
            emptylayout1.setVisibility(View.VISIBLE);
            btnReload1.setVisibility(View.VISIBLE);
            img_defaultfail1.setBackgroundResource(R.drawable.defailt_fail);
        } else {
            if (mStoryAdapter.getCount() == 0) {
                emptylayout1.setVisibility(View.VISIBLE);
                btnReload1.setVisibility(View.GONE);
                img_defaultfail1.setBackgroundResource(R.drawable.empty_mylove);
            } else {
                emptylayout1.setVisibility(View.GONE);
            }
        }
    }

    //
    private void fillPhotoList(boolean loadFail) {
        if (mRadioAdapter == null) {
            if (mRadioArrayList != null && !mRadioArrayList.isEmpty()) {
                mRadioAdapter = new ImageGridAdapter2(this, mRadioArrayList);
            } else {
                mRadioAdapter = new ImageGridAdapter2(this,
                        new ArrayList<PhotoItem>());
            }

            listView2.setAdapter(mRadioAdapter);
        } else {
            mRadioAdapter.notifyDataSetChanged();

            if (mRadioArrayList != null
                    && mRadioArrayList.size() == mLeFavoriteItemWrapper2.total) {
                refreshLayout2.refreshFinish(PullToRefreshLayout.LOAD_COMPLETED);
                refreshLayout2.loadmoreFinish(PullToRefreshLayout.LOAD_COMPLETED);
            } else {
                refreshLayout2.refreshFinish(PullToRefreshLayout.SUCCEED);
                refreshLayout2.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }

            // refreshLayout2.refreshFinish(PullToRefreshLayout.SUCCEED);
            // refreshLayout2.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }

        if (loadFail) {
            emptylayout2.setVisibility(View.VISIBLE);
            btnReload2.setVisibility(View.VISIBLE);
            img_defaultfail2.setBackgroundResource(R.drawable.defailt_fail);
        } else {
            if (mRadioAdapter.getCount() == 0) {
                emptylayout2.setVisibility(View.VISIBLE);
                btnReload2.setVisibility(View.GONE);
                img_defaultfail2.setBackgroundResource(R.drawable.empty_mylove);
            } else {
                emptylayout2.setVisibility(View.GONE);
            }
        }
    }


//    private void loadDataByMonth(final String strDate) {
//        // showloading("");
//
//        new CustomAsyncTask(getActivity(), new IAsyncTask() {
//            @SuppressLint("SimpleDateFormat")
//            @Override
//            public void onRecieveData(Activity activity, ResponseResult arg0) {
//                if (arg0.isSuccess()) {
//                    if (Tools.isNotNullStr(arg0.data)) {
//                        if (Tools.isNotEmpty(arg0) && Tools.isNotEmpty(arg0.data)) {
//                            mList.clear();
//                            try {
//                                JSONObject result = new JSONObject(arg0.data);
//                                Iterator<?> it = result.keys();
//                                String keyDate = null;
//                                while (it.hasNext()) {// 遍历JSONObject
//                                    keyDate = it.next().toString();
//
//                                    if (Tools.isNotEmpty(keyDate)) {
//                                        JSONArray playList = result.getJSONArray(keyDate);// 获取JSONArray
//                                        if (Tools.isNotEmpty(playList)) {
//                                            ArrayList<HabitPlan> list = (ArrayList<HabitPlan>) JsonSerializer
//                                                    .getInstance().deserialize(playList.toString(),
//                                                            ArrayList.class, HabitPlan.class);
//                                            if (Tools.isNotEmpty(list)) {
//                                                LeHabitPlanRecordWrapper itemVo = new LeHabitPlanRecordWrapper();
//                                                itemVo.dateTag = keyDate;// );
//                                                itemVo.list = list; // );
//                                                mList.add(itemVo);
//                                            }
//                                        }
//                                    }
//                                }
//
//                            } catch (JSONException e) {
//                                e.printStackTrace();
//                            }
//
//                            getActivity().runOnUiThread(new Runnable() {
//
//                                @Override
//                                public void run() {
//                                    setAdapter();
//                                }
//                            });
//
//                        }
//                    }
//                }
//
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                String strToday = sdf.format(Calendar.getInstance().getTime());
//
////        if (strDate.compareTo(strToday) < 0) {
////          dateStatus = -1;
////        } else if (strDate.compareTo(strToday) == 0) {
////          dateStatus = 0;
////        } else if (strDate.compareTo(strToday) > 0) {
////          dateStatus = 1;
////        }
////        updateBeginClassBtn();
////
////        checkAnimAndTaskNumber(strDate);
//
//                isPageChanged = false;
//                //  dismissLoading();
//            }
//
//            @Override
//            public ResponseResult doInbackground(Activity activity) {
//                if (mLeUser == null) {
//                    mLeUser = Tools.restoreLeUser();
//                }
//                HashMap<String, Object> param_ = new HashMap<String, Object>();
//                param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
//                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
//                param_.put("date", strDate);
//
//                return HttpUtils.startRequest(
//                        HttpUtils.API_URL + "/api/2.0/operation/habit/habits_teacher", param_,
//                        HttpUtils.TAG_GET_REQUEST);
//            }
//        }).execute();
//    }

    // 习惯
    private void loadStoryData() {
        new CustomAsyncTask(this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
                if (result.isSuccess() && Tools.isNotNullStr(result.data)) {
                    if (mStoryAdapterList != null) {
                        mStoryAdapterList.clear();
                    }
                    if (isrefreshlist1) {
                        mStoryArrayList.clear();
                    }

                    mLeFavoriteItemWrapper1 = JsonSerializer.getInstance().deserialize(
                            result.data, LeBabyHabitWrapper.class);

                    mStoryAdapterList = (ArrayList<BabyHabit>) mLeFavoriteItemWrapper1.data;
                    if (Tools.isNotEmpty(mStoryAdapterList)) {
                        mStoryArrayList.addAll(mStoryAdapterList);
                    }
//
                    fillLoveStoryList(false);
                } else {
//                    mStoryArrayList.clear();
                    fillLoveStoryList(true);
                }


            }

            @Override
            public ResponseResult doInbackground(Activity activity) {
                if (mLeUser == null) {
                    mLeUser = Tools.restoreLeUser();
                }

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String mCurDay = sdf.format(Calendar.getInstance().getTime());


                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                param_.put("macid", mStudentDeviceModel.machine_id);

                if (Tools.isNotEmpty(mCurDay)) {
                    param_.put("date", mCurDay.substring(0, 7));
                }

                return HttpUtils.startRequest(
                        HttpUtils.API_URL + "/api/2.0/operation/teacher/habit/get_habits_for_mac", param_,
                        HttpUtils.TAG_GET_REQUEST);
            }
        }).execute();
    }

    private void loadPhotoData() {
        new CustomAsyncTask(this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
                if (result.isSuccess() && Tools.isNotNullStr(result.data)) {
                    if (mRadioAdapterList != null) {
                        mRadioAdapterList.clear();
                    }
                    if (isrefreshlist2) {
                        mRadioArrayList.clear();
                    }

                    mLeFavoriteItemWrapper2 = JsonSerializer.getInstance().deserialize(
                            result.data, LePhotoItemWrapper.class);
                    mRadioAdapterList = (ArrayList<PhotoItem>) mLeFavoriteItemWrapper2.data;
                    if (Tools.isNotEmpty(mRadioAdapterList)) {
                        mRadioArrayList.addAll(mRadioAdapterList);
                    }

                    fillPhotoList(false);
                } else {
                    mRadioArrayList.clear();
                    fillPhotoList(true);
                }

                isrefreshlist2 = false;
            }

            @Override
            public ResponseResult doInbackground(Activity activity) {
                HashMap<String, Object> param_ = new HashMap<String, Object>();
                param_.put(HttpUtils.TAG_PAGE_I, mLastpage2);
                param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
                param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                param_.put("macid", mStudentDeviceModel.machine_id);
                param_.put("page_size", 9);
                return HttpUtils.startRequest(HttpUtils.API_URL
                                + "/api/2.0/teacher/teacher_photo", param_,
                        HttpUtils.TAG_GET_REQUEST);
            }
        }).execute();
    }

    private void initialize() {
        mLeUser = MyTeacherApp.getInstance().getUserTeacher();

        mStoryAdapterList = new ArrayList<>();
        mRadioAdapterList = new ArrayList<>();
//
        mStoryArrayList = new ArrayList<>();
        mRadioArrayList = new ArrayList<>();

        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm); // 得到 屏幕的宽度
        screenW = dm.widthPixels;
        params = beacon.getLayoutParams();
        params.width = screenW / 2; // 设置 控件 cursor 图片的 宽度 是 屏宽的 2/5
        beacon.setLayoutParams(params);
        views = new ArrayList<View>(); // 布局集合
        mInflater = LayoutInflater.from(this);

        page1 = mInflater.inflate(R.layout.pagelayout3_teacher, null);
        emptylayout1 = (LinearLayout) page1.findViewById(R.id.emptylayout);
        reloadRippleView1 = (RippleView) page1.findViewById(R.id.reloadRippleView);
        refreshLayout1 = (PullToRefreshLayout) page1
                .findViewById(R.id.refresh_view);

        img_defaultfail1 = (ImageView) page1.findViewById(R.id.img_defaultfail);
        btnReload1 = (Button) page1.findViewById(R.id.reload);
        listView1 = (ListView) page1.findViewById(R.id.content_views);

        listView1.setClickable(false);

        page2 = mInflater.inflate(R.layout.pagelayout4_teacher, null);
        emptylayout2 = (LinearLayout) page2.findViewById(R.id.emptylayout);
        reloadRippleView2 = (RippleView) page2.findViewById(R.id.reloadRippleView);
        refreshLayout2 = (PullToRefreshLayout) page2
                .findViewById(R.id.refresh_view);
        listView2 = (PullableGridView) page2.findViewById(R.id.content_views);
        img_defaultfail2 = (ImageView) page2.findViewById(R.id.img_defaultfail);
        btnReload2 = (Button) page2.findViewById(R.id.reload);

        views.add(page1);
        views.add(page2);

        offset = screenW / 2;
    }

    private void InitViewPager() {
        vpager.setAdapter(new MyPagerAdapter(views));

        if (isphoto) {
            vpager.setCurrentItem(1);
            currIndex = 1;
        } else {
            vpager.setCurrentItem(0);
            currIndex = 0;
        }


        if (currIndex == 0) {
            tv_edittt.setVisibility(View.GONE);
        } else {
            tv_edittt.setVisibility(View.VISIBLE);
        }
    }

    private void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
    }

    private void initListener() {
        vpager.setOnPageChangeListener(this);
        refreshLayout1.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                mLastpage = 1;
                isrefreshlist1 = true;
                loadStoryData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                //  mLastpage++;
                isrefreshlist1 = false;

                refreshLayout1.refreshFinish(PullToRefreshLayout.LOAD_COMPLETED);
                refreshLayout1.loadmoreFinish(PullToRefreshLayout.LOAD_COMPLETED);

                //   loadStoryData();
            }
        });

        refreshLayout2.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                mLastpage2 = 1;
                isrefreshlist2 = true;
                loadPhotoData();
                //  loadRadioData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                mLastpage2++;
                isrefreshlist2 = false;
                loadPhotoData();
                //  loadRadioData();
            }
        });

        reloadRippleView1
                .setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                    @Override
                    public void onComplete(RippleView rippleView) {
                        mLastpage = 1;
                        isrefreshlist1 = true;
                        loadStoryData();
                    }
                });

        reloadRippleView2
                .setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                    @Override
                    public void onComplete(RippleView rippleView) {
                        mLastpage2 = 1;
                        isrefreshlist2 = true;
                        loadPhotoData();
                    }
                });

        listView1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
//                Intent intent = new Intent();
//                intent.setClass(MyLoveListActivity.this, StoryDetailActivity.class);
////                intent.putExtra("id", mStoryArrayList.get(arg2).episode.album_id);
////                intent.putExtra("sort", mStoryArrayList.get(arg2).episode.sort); // 第几集，高亮
//                startActivity(intent);
            }
        });

        listView2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {


                if (!bottomlyts.isShown()) {
                    Intent intent = new Intent(MyLoveListActivity.this,
                            ImageZoomActivity2.class);
                    intent.putExtra(IntentConstants.EXTRA_IMAGE_LIST, mRadioArrayList);
                    intent.putExtra(IntentConstants.EXTRA_CURRENT_IMG_POSITION, arg2);
                    startActivity(intent);
                } else {
                    PhotoItem item = mRadioArrayList.get(arg2);
                    if (item.isChecked) {
                        item.isChecked = false;
                        selectedImgs.remove(item.id);
                    } else {
//                        if (selectedImgs.size() >= availableSize) {
//                            Toast.makeText(ImageChooseActivity.this,
//                                    "最多选择" + availableSize + "张图片", Toast.LENGTH_SHORT).show();
//                            return;
//                        }
                        item.isChecked = true;
                        selectedImgs.put(item.id, item);
                    }

                    mRadioAdapter.notifyDataSetChanged();
                }


//                Intent intent = new Intent();
//                intent.setClass(MyLoveListActivity.this, StoryDetailActivity.class);
////                intent.putExtra("id", mRadioArrayList.get(arg2).episode.album_id);
////                intent.putExtra("sort", mRadioArrayList.get(arg2).episode.sort); // 第几集，高亮
//                startActivity(intent);
            }
        });
    }

    private HashMap<Integer, PhotoItem> selectedImgs = new HashMap<Integer, PhotoItem>();

    private void initUpRightBtn() {
        getbtn_right().setVisibility(View.VISIBLE);
        getbtn_right().setBackgroundResource(R.drawable.edit_btn);
        getbtn_right().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (!bottomlyts.isShown()) {
//                    bottomlyts.setVisibility(View.VISIBLE);
//                    getbtn_right().setBackgroundResource(R.drawable.cancle);
//                    vpager.setScanScroll(false);
//                    if (currIndex == 0 && mStoryAdapter != null) {
//                        mStoryAdapter.setEditor(true);
//                        mStoryAdapter.notifyDataSetChanged();
//                    } else if (currIndex == 1 && mRadioAdapter != null) {
//                        mRadioAdapter.setEditor(true);
//                        mRadioAdapter.notifyDataSetChanged();
//                    }
//
//                    toplyt.setVisibility(View.GONE);
//                    bealyt.setVisibility(View.GONE);
//                    mBackImageButton.setVisibility(View.INVISIBLE);
//                } else {
//                    bottomlyts.setVisibility(View.GONE);
//                    vpager.setScanScroll(true);
//                    if (currIndex == 0 && mStoryAdapter != null) {
//                        mStoryAdapter.setEditor(false);
//                        mStoryAdapter.notifyDataSetChanged();
//
//                        if (mStoryArrayList != null) {
//                            for (LeFavoriteItem item : mStoryArrayList) {
//                                item.isChecked = false;
//                            }
//                        }
//                    } else if (currIndex == 1 && mRadioAdapter != null) {
//                        mRadioAdapter.setEditor(false);
//
//                        if (mRadioArrayList != null) {
//                            for (LeFavoriteItem item : mRadioArrayList) {
//                                item.isChecked = false;
//                            }
//                        }
//
//                        mRadioAdapter.notifyDataSetChanged();
//                    }
//
//                    toplyt.setVisibility(View.VISIBLE);
//                    bealyt.setVisibility(View.VISIBLE);
//                    getbtn_right().setBackgroundResource(R.drawable.edit_btn);
//                    mBackImageButton.setVisibility(View.VISIBLE);
//                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_face_img:
            case R.id.parentAccountIdTextView:
            case R.id.tv_desc:
                final CustomEditDialogOne.Builder customBuilder = new CustomEditDialogOne.Builder(
                        this);
                customBuilder.setTitle("修改设备昵称")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, int which) {
                        final String nickname = customBuilder.getEditText();

                        if (Tools.isNotEmpty(nickname)) {
                            // 修改昵称
                            new CustomAsyncTask(MyLoveListActivity.this, new IAsyncTask() {
                                @Override
                                public void onRecieveData(Activity activity, ResponseResult result) {
                                    if (result.isSuccess()) {
                                        if (dialog != null) {
                                            dialog.dismiss();
                                        }
                                        // getBindDevices();

                                        parentAccountIdTextView.setText(nickname);
                                    }
                                }

                                @Override
                                public ResponseResult doInbackground(Activity activity) {
                                    if (mLeUser == null) {
                                        mLeUser = Tools.restoreLeUser();
                                    }

                                    HashMap<String, Object> param_ = new HashMap<String, Object>();

                                    param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
                                    param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                                    param_.put("nick_name", nickname);

                                    return HttpUtils.startRequest(HttpUtils.HOST_API
                                                    + "/api/2.0/teacher/teacher_machine/" + mStudentDeviceModel.id, param_,
                                            HttpUtils.TAG_PUT_REQUEST);
                                }
                            }).execute();
                        }
                    }
                });

                if (!isFinishing()) {
                    customBuilder.create().show();
                }

                break;
            case R.id.tv_edittt:
                if (!bottomlyts.isShown()) {
                    bottomlyts.setVisibility(View.VISIBLE);
                    tv_edittt.setText("取消");
                    vpager.setScanScroll(false);
                   /* if (currIndex == 0 && mStoryAdapter != null) {
                        mStoryAdapter.setEditor(true);
                        mStoryAdapter.notifyDataSetChanged();
                    } else*/
                    if (currIndex == 1 && mRadioAdapter != null) {
                        mRadioAdapter.setEditable(true);
                        mRadioAdapter.notifyDataSetChanged();
                    }
                } else {
                    bottomlyts.setVisibility(View.GONE);
                    vpager.setScanScroll(true);
                    /*if (currIndex == 0 && mStoryAdapter != null) {
                        mStoryAdapter.setEditor(false);
                        mStoryAdapter.notifyDataSetChanged();

                        if (mStoryArrayList != null) {
                            for (LeFavoriteItem item : mStoryArrayList) {
                                item.isChecked = false;
                            }
                        }
                    } else */
                    if (currIndex == 1 && mRadioAdapter != null) {
                        mRadioAdapter.setEditable(false);

                        if (mRadioArrayList != null) {
                            for (PhotoItem item : mRadioArrayList) {
                                item.isChecked = false;
                            }
                        }

                        mRadioAdapter.notifyDataSetChanged();
                    }

                    tv_edittt.setText("编辑");
                }
                break;
            case R.id.select_cartoon:
                vpager.setCurrentItem(0);
//                if (mStoryAdapter != null)
//                    mStoryAdapter.notifyDataSetChanged();
                break;
            case R.id.select_radio:
                vpager.setCurrentItem(1);
//                if (mRadioAdapter != null)
//                    mRadioAdapter.notifyDataSetChanged();
                break;
            case R.id.delete:
                deleteRadio();
//                if (currIndex == 0) {
//                    deleteStory();
//                } else {
//                    deleteRadio();
//                }
                break;
//            case R.id.select:
//                if (select.getText().toString().equals("全选")) {
////                    if (currIndex == 0 && mStoryAdapter != null) {
////                        mStoryAdapter.setSelectMode(SelectionMode.SELECT_ALL);
////                        mStoryAdapter.notifyDataSetChanged();
////                    } else
// if (currIndex == 1 && mRadioAdapter != null) {
//                        mRadioAdapter.setSelectMode(SelectionMode.SELECT_ALL);
//                        mRadioAdapter.notifyDataSetChanged();
//                    }
//
//                    select.setText("取消全选");
//                } else if (select.getText().toString().equals("取消全选")) {
////                    if (currIndex == 0 & mStoryAdapter != null) {
////                        mStoryAdapter.setSelectMode(SelectionMode.DESELECT_ALL);
////                        mStoryAdapter.notifyDataSetChanged();
////                    } else if (currIndex == 1 && mRadioAdapter != null) {
////                        mRadioAdapter.setSelectMode(SelectionMode.DESELECT_ALL);
////                        mRadioAdapter.notifyDataSetChanged();
////                    }
//
//                    select.setText("全选");
//                }
//                break;
            default:
                break;
        }
    }

    private void deleteRadio() {
        int selectCount = 0;
        if (mRadioArrayList != null) {
            for (PhotoItem item : mRadioArrayList) {
                if (item != null && item.isChecked) {
                    ++selectCount;
                }
            }
        }

        if (selectCount > 0) {
            CustomCommonDialog.Builder cdb = new CustomCommonDialog.Builder(
                    MyLoveListActivity.this).setMessage("确定删除吗？").setTitle("删除提示")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {

                            if (mRadioArrayList != null && !mRadioArrayList.isEmpty()) {
                                final List<PhotoItem> selectedRecordings = new ArrayList<PhotoItem>();
                                String storyIds = "";

                                for (PhotoItem item : mRadioArrayList) {
                                    if (item.isChecked) {
                                        selectedRecordings.add(item);
                                        storyIds += item.id;
                                        storyIds += ",";
                                    }
                                }

                                if (Tools.isNotEmpty(storyIds)) {
                                    storyIds = storyIds.substring(0, storyIds.length() - 1);
                                }

                                if (selectedRecordings.size() > 0) {
                                    showloading("");
                                    final String finalStoryIds = storyIds;
                                    new CustomAsyncTask(MyLoveListActivity.this,
                                            new IAsyncTask() {
                                                @Override
                                                public void onRecieveData(Activity activity,
                                                                          ResponseResult result) {

                                                    if (result != null && result.isSuccess()) {
                                                        for (PhotoItem rItem : selectedRecordings) {
                                                            mRadioAdapter.remove(rItem);
                                                        }
                                                        mRadioAdapter.removeSelection();

                                                        if (Tools.isNotNullStr(result.data)) {
                                                            Toast.makeText(MyLoveListActivity.this,
                                                                    "删除成功!", Toast.LENGTH_SHORT).show();
                                                        }
                                                    }

                                                    dismissLoading();
                                                }

                                                @Override
                                                public ResponseResult doInbackground(Activity activity) {
                                                    if (mLeUser == null) {
                                                        mLeUser = Tools.restoreLeUser();
                                                    }

                                                    HashMap<String, Object> param_ = new HashMap<String, Object>();
                                                    param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
                                                    param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                                                    return HttpUtils.startRequest(HttpUtils.HOST_API
                                                                    + "/api/2.0/teacher/teacher_photo/" + finalStoryIds,
                                                            param_, HttpUtils.TAG_DELETE_REQUEST);
                                                }
                                            }).execute();

                                }
                                dialog.dismiss();
                            }
                        }
                    });
            cdb.create().show();
        } else {
            toastShow("未选中任何条目!");
        }
    }
//
//    private void deleteStory() {
//        int selectCount = 0;
//        if (mStoryArrayList != null) {
//            for (LeFavoriteItem item : mStoryArrayList) {
//                if (item != null && item.isChecked) {
//                    ++selectCount;
//                }
//            }
//        }
//
//        if (selectCount > 0) {
//            CustomCommonDialog.Builder cdb = new CustomCommonDialog.Builder(
//                    MyLoveListActivity.this).setMessage("确定删除吗？").setTitle("删除提示")
//                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
//                        }
//                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int id) {
//
//                            if (mStoryArrayList != null && !mStoryArrayList.isEmpty()) {
//                                final List<LeFavoriteItem> selectedRecordings = new ArrayList<LeFavoriteItem>();
//                                String storyIds = "";
//
//                                for (LeFavoriteItem item : mStoryArrayList) {
//                                    if (item.isChecked) {
//                                        selectedRecordings.add(item);
//                                        storyIds += item.id;
//                                        storyIds += ";";
//                                    }
//                                }
//
//                                if (Tools.isNotEmpty(storyIds)) {
//                                    storyIds = storyIds.substring(0, storyIds.length() - 1);
//                                }
//
//                                if (selectedRecordings.size() > 0) {
//                                    showloading("");
//                                    final String finalStoryIds = storyIds;
//                                    new CustomAsyncTask(MyLoveListActivity.this,
//                                            new IAsyncTask() {
//                                                @Override
//                                                public void onRecieveData(Activity activity,
//                                                                          ResponseResult result) {
//
//                                                    dismissLoading();
//                                                    // if (result != null && result.isSuccess()) {
//                                                    for (LeFavoriteItem rItem : selectedRecordings) {
//                                                        mStoryAdapter.remove(rItem);
//                                                    }
//                                                    mStoryAdapter.removeSelection();
//
//                                                    if (Tools.isNotNullStr(result.data)) {
//                                                        Toast.makeText(MyLoveListActivity.this,
//                                                                result.data, Toast.LENGTH_SHORT).show();
//                                                    }
//                                                    // }
//                                                }
//
//                                                @Override
//                                                public ResponseResult doInbackground(Activity activity) {
//                                                    if (mLeUser == null) {
//                                                        mLeUser = Tools.restoreLeUser();
//                                                    }
//
//                                                    HashMap<String, Object> param_ = new HashMap<String, Object>();
//                                                    param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
//                                                    param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
//                                                    return HttpUtils.startRequest(HttpUtils.HOST_API
//                                                                    + "/api/2.0/member/favorite/" + finalStoryIds,
//                                                            param_, HttpUtils.TAG_DELETE_REQUEST);
//                                                }
//                                            }).execute();
//
//                                }
//                                dialog.dismiss();
//                            }
//                        }
//                    });
//            cdb.create().show();
//        } else {
//            toastShow("未选中任何条目!");
//        }
//    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int arg0) {
        Animation animation = null;
        switch (arg0) {
            case 0:
                if (currIndex == 1) {
                    animation = new TranslateAnimation(offset, 0, 0, 0);
                }

                tv_edittt.setVisibility(View.GONE);

//                if (mStoryAdapter != null)
//                    mStoryAdapter.notifyDataSetChanged();

                //  bottomlyts.setVisibility(View.GONE);
                tv_cartoon.setTextColor(0xff2bacfc);
                tv_radio.setTextColor(this.getResources().getColor(R.color.text_dark_gray));

                break;
            case 1:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(0, offset, 0, 0);
                }
                tv_edittt.setVisibility(View.VISIBLE);

//                if (mRadioAdapter != null)
//                    mRadioAdapter.notifyDataSetChanged();

                //  bottomlyts.setVisibility(View.VISIBLE);
                tv_cartoon.setTextColor(this.getResources().getColor(R.color.text_dark_gray));
                tv_radio.setTextColor(0xff2bacfc);

                break;
        }
        currIndex = arg0;

        if (animation != null) {
            animation.setFillAfter(true);
            animation.setDuration(200);
            beacon.startAnimation(animation);
        }
    }

}
